import request from '@/utils/request'

// 查询房屋列表
export function listHouse(query) {
  return request({
    url: '/house/house/list',
    method: 'get',
    params: query
  })
}

// 查询房屋详细
export function getHouse(houseId) {
  return request({
    url: '/house/house/' + houseId,
    method: 'get'
  })
}

// 新增房屋
export function addHouse(data) {
  const formData = new FormData();
  
  // 添加房屋基本信息
  const { photos, ...houseData } = data;
  formData.append('house', new Blob([JSON.stringify(houseData)], {
    type: 'application/json'
  }));
  
  // 添加照片文件
  if (photos && photos.length > 0) {
    console.log('准备上传的照片数量:', photos.length);
    photos.forEach((photo, index) => {
      console.log(`处理第${index + 1}张照片:`, photo);
      formData.append('photos', photo);
    });
  }

  // 打印FormData内容
  console.log('FormData内容:');
  for (let [key, value] of formData.entries()) {
    console.log(key, value);
  }

  return request({
    url: '/house/house',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 修改房屋
export function updateHouse(data) {
  const formData = new FormData();
  
  // 添加房屋基本信息
  const { photos, deletedPhotoIds, ...houseData } = data;
  formData.append('house', new Blob([JSON.stringify(houseData)], {
    type: 'application/json'
  }));
  
  // 添加照片文件
  if (photos && photos.length > 0) {
    photos.forEach(photo => {
      formData.append('photos', photo);
    });
  }

  // 添加待删除的照片ID
  if (deletedPhotoIds && deletedPhotoIds.length > 0) {
    deletedPhotoIds.forEach(id => {
      formData.append('deletedPhotoIds', id);
    });
  }

  return request({
    url: '/house/house',
    method: 'put',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 删除房屋
export function delHouse(houseId) {
  return request({
    url: '/house/house/' + houseId,
    method: 'delete'
  })
} 